// Decompiled by Jad v1.5.7f. Copyright 2000 Pavel Kouznetsov.
// Jad home page: http://www.geocities.com/SiliconValley/Bridge/8617/jad.html
// Decompiler options: packimports(3) 
// Source File Name:   ReportServiceImp.java

package com.iphaeton.lms.business.report;

import com.iphaeton.exception.BusinessException;
import com.iphaeton.exception.PersistenceException;
import com.iphaeton.lms.business.PersistentServiceSupport;
import com.iphaeton.lms.business.course.scorm12.ICourseService;
import com.iphaeton.lms.business.organize.IOrganizeService;
import com.iphaeton.lms.pojo.course.Course;
import com.iphaeton.lms.pojo.course.CourseSort;
import com.iphaeton.lms.pojo.organize.Organize;
import com.iphaeton.lms.pojo.organize.User;
import com.iphaeton.persistence.IPersistenceService;
import com.iphaeton.persistence.spring.JdbcPService;
import java.sql.*;
import java.util.*;
import org.dom4j.*;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.CallableStatementCallback;
import org.springframework.jdbc.core.JdbcTemplate;

// Referenced classes of package com.iphaeton.lms.business.report:
//            IReportService

public class ReportServiceImp extends PersistentServiceSupport
    implements IReportService
{

    public ReportServiceImp()
    {
        organize = null;
        course = null;
    }

    public IOrganizeService getOrganize()
    {
        return organize;
    }

    public void setOrganize(IOrganizeService organize)
    {
        this.organize = organize;
    }

    public Document getTreeRoot(String webRoot, Organize org, String type)
        throws BusinessException
    {
        if(org == null)
        {
            throw new BusinessException("null org isn't allowed in function getOrganizeTree(Organize org)!");
        } else
        {
            Document doc = DocumentHelper.createDocument();
            Element element = doc.addElement("tree");
            addOrganizeNode(webRoot, element, org, type);
            return doc;
        }
    }

    private Element addOrganizeNode(String webRoot, Element parent, Organize org, String type)
    {
        Element child = parent.addElement("tree");
        child.addAttribute("text", org.getName());
        String id = org.getId();
        if("course".equals(type))
        {
            try
            {
                if(!organize.isLeaf(org))
                {
                    child.addAttribute("src", webRoot + "/reportTxAction.do?method=getTree&type=course&id=" + id);
                    child.addAttribute("icon", webRoot + "/images/xtree/course1.gif");
                } else
                {
                    child.addAttribute("icon", webRoot + "/images/xtree/course.gif");
                }
            }
            catch(BusinessException e)
            {
                e.printStackTrace();
            }
            child.addAttribute("action", webRoot + "/reportTxAction.do?method=organizeCourseReport&id=" + id);
            child.addAttribute("target", "operateFrame");
            child.addAttribute("expended", "false");
        }
        return child;
    }

    public Document getCourseTree(String webRoot, Organize org)
        throws BusinessException
    {
        if(org == null)
            throw new BusinessException("null org isn't allowed in function getCourseTree(Organize org)!");
        Document doc = DocumentHelper.createDocument();
        Element element = doc.addElement("tree");
        if(!organize.isLeaf(org))
        {
            List list = organize.findChildOrganize(org);
            for(Iterator it = list.iterator(); it.hasNext();)
            {
                Organize o = (Organize)it.next();
                if(!organize.isRoot(o))
                    addOrganizeNode(webRoot, element, o, "course");
            }

        }
        return doc;
    }

    public List organizeCourseReport(String url)
        throws BusinessException
    {
       final String organize_url = url;
        List list = null;
        CallableStatementCallback cb = new CallableStatementCallback() {

            public Object doInCallableStatement(CallableStatement cs)
                throws SQLException, DataAccessException
            {
                List list = new LinkedList();
                cs.setString(1, organize_url);
                cs.execute();
                ResultSet rs;
                Map map;
                for(rs = cs.getResultSet(); rs.next(); list.add(map))
                {
                    map = new HashMap();
                    map.put("id", rs.getString("id"));
                    map.put("name", rs.getString("cname"));
                    map.put("sort", rs.getString("sname"));
                    map.put("hour", rs.getString("hour"));
                    map.put("criterion", rs.getString("criterion"));
                    map.put("count", rs.getString("count"));
                }

                rs.close();
                return list;
            }

        };
        try
        {
            list = (List)jdbcpservice.getSpringJdbcTemplate().execute("{call pro_organize_course(?)}", cb);
        }
        catch(DataAccessException e)
        {
            e.printStackTrace();
            throw new BusinessException("\u67E5\u8BE2\u7EC4\u7EC7\u8BFE\u7A0B\u65F6\u51FA\u9519: " + e.getMessage());
        }
        catch(PersistenceException e)
        {
            e.printStackTrace();
            throw new BusinessException("\u67E5\u8BE2\u7EC4\u7EC7\u8BFE\u7A0B\u65F6\u51FA\u9519: " + e.getMessage());
        }
        return list;
    }

    public List organizeCourseChart(String id)
        throws BusinessException
    {
        List list = null;
        final Organize org = organize.findOrganizeById(id);
        final int oc = organize.findAllUser(org, 'a').size();
        CallableStatementCallback cb = new CallableStatementCallback() {

            public Object doInCallableStatement(CallableStatement cs)
                throws SQLException, DataAccessException
            {
                List list = new LinkedList();
                cs.setString(1, org.getUrl());
                cs.execute();
                ResultSet rs = cs.getResultSet();
                StringBuffer course = new StringBuffer();
                List count = new LinkedList();
                for(; rs.next(); count.add(new Integer(rs.getInt("count"))))
                {
                    course.append(rs.getString("cname"));
                    course.append(",");
                }

                list.add(0, course);
                list.add(1, count);
                list.add(2, org.getUrl() + "  \u5171 [ " + oc + " ] \u4EBA,");
                rs.close();
                return list;
            }

        };
        try
        {
            list = (List)jdbcpservice.getSpringJdbcTemplate().execute("{call pro_organize_course(?)}", cb);
        }
        catch(DataAccessException e)
        {
            e.printStackTrace();
            throw new BusinessException("\u67E5\u8BE2\u7EC4\u7EC7\u8BFE\u7A0B\u65F6\u51FA\u9519: " + e.getMessage());
        }
        catch(PersistenceException e)
        {
            e.printStackTrace();
            throw new BusinessException("\u67E5\u8BE2\u7EC4\u7EC7\u8BFE\u7A0B\u65F6\u51FA\u9519: " + e.getMessage());
        }
        return list;
    }

    public List courseStudyChart(String id, String course_id, String sort)
        throws BusinessException
    {
        List list = new LinkedList();
        List period = new LinkedList();
        StringBuffer usb = new StringBuffer();
        StringBuffer csb = new StringBuffer();
        String hql1 = "select distinct(uc.user) from UserCourse uc where uc.type=1 and uc.state=1";
        String hql2 = "select distinct(uc.course) from UserCourse uc where uc.type=1 and uc.state=1";
        if(sort != null && !"".equals(sort))
        {
            hql1 = hql1 + " and uc.course.sortId='" + sort + "'";
            hql2 = hql2 + " and uc.course.sortId='" + sort + "'";
        } else
        if(course_id != null && !"".equals(course_id))
        {
            hql1 = hql1 + " and uc.courseId='" + course_id + "'";
            hql2 = hql2 + " and uc.course.id='" + course_id + "'";
        }
        try
        {
            List ulist = hbpservice.query(hql1);
            List clist = hbpservice.query(hql2);
            int count = 0;
            for(int i = 0; i < clist.size(); i++)
            {
                Course c = (Course)clist.get(i);
                csb.append(c.getName() + ",");
                for(int j = 0; j < ulist.size(); j++)
                {
                    User u = (User)ulist.get(j);
                    period.add(count, new Integer(Math.round(course.getPeriod(u.getId(), c.getId()))));
                    count++;
                }

            }

            for(int j = 0; j < ulist.size(); j++)
            {
                User u = (User)ulist.get(j);
                usb.append(u.getName() + "[" + u.getUserId() + "],");
            }

            list.add(0, usb);
            list.add(1, period);
            list.add(2, csb);
        }
        catch(PersistenceException e)
        {
            e.printStackTrace();
            throw new BusinessException("\u67E5\u8BE2\u5B66\u5458\u5B66\u4E60\u62A5\u8868\u51FA\u9519!");
        }
        return list;
    }

    public Document getCourseTree(String id)
        throws BusinessException
    {
        Document doc = DocumentHelper.createDocument();
        Element element = doc.addElement("tree");
        List list = course.findCourseSort();
        for(int i = 0; i < list.size(); i++)
        {
            CourseSort cs = (CourseSort)list.get(i);
            Element csElement = element.addElement("tree");
            csElement.addAttribute("text", cs.getName());
            List courseList = course.findCourse("a", id, cs.getId());
            if(courseList != null && courseList.size() > 0)
                csElement.addAttribute("src", "../../reportTxAction.do?method=sortCourseTree&id=" + cs.getId());
            csElement.addAttribute("action", "../../reportTxAction.do?method=courseStudyReport&course_sort=" + cs.getId());
            csElement.addAttribute("icon", "../../images/xtree/course1.gif");
            csElement.addAttribute("target", "operateFrame");
            csElement.addAttribute("expended", "false");
        }

        return doc;
    }

    public Document getSortCourseTree(String organize_id, String id)
        throws BusinessException
    {
        Document doc = DocumentHelper.createDocument();
        Element element = doc.addElement("tree");
        List list = course.findCourse("a", organize_id, id);
        for(int i = 0; i < list.size(); i++)
        {
            Course c = (Course)list.get(i);
            Element cElement = element.addElement("tree");
            cElement.addAttribute("text", c.getName());
            cElement.addAttribute("action", "../../reportTxAction.do?method=courseStudyReport&course_id=" + c.getId());
            cElement.addAttribute("icon", "../../images/xtree/course.gif");
            cElement.addAttribute("target", "operateFrame");
            cElement.addAttribute("expended", "false");
        }

        return doc;
    }

    public ICourseService getCourse()
    {
        return course;
    }

    public void setCourse(ICourseService course)
    {
        this.course = course;
    }

    protected IOrganizeService organize;
    protected ICourseService course;
}
